package com.extr.domain.question;

import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("QuestionContent")
public class QuestionContent {

	private static final String regEx_html = "<[^>]+>";
	private static final String regEx_vbscript = "Private (Sub|Function)([\\w\\W]*)End (Sub|Function)";
	private static Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
	private static Pattern p_vbscript = Pattern.compile(regEx_vbscript, Pattern.CASE_INSENSITIVE);
	@XStreamAlias("title")
	private String title;
	@XStreamAlias("titleImg")
	private String titleImg = "";
	@XStreamAlias("choiceList")
	private LinkedHashMap<String, String> choiceList;
	@XStreamAlias("choiceImgList")
	private LinkedHashMap<String, String> choiceImgList;

	public String getTitle() {
		return title;
	}

	public String getXmpTitle(){
		Matcher matcher = p_vbscript.matcher(title);
		if(matcher.find()){
			return String.format("%s<xmp>%s</xmp>",matcher.replaceFirst(""),matcher.group());
		}
		matcher = p_html.matcher(title);
		if(matcher.find()) {
			return String.format("<xmp>%s</xmp>", title);
		}
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitleImg() {
		return titleImg;
	}

	public void setTitleImg(String titleImg) {
		this.titleImg = titleImg;
	}

	public LinkedHashMap<String, String> getChoiceList() {
		return choiceList;
	}

	public String getChoiceValue(String key){
		String value = choiceList.get(key);
		Matcher matcher = p_vbscript.matcher(value);
		if(matcher.find()){
			return String.format("%s<xmp>%s</xmp>",matcher.replaceFirst(""),matcher.group());
		}
		matcher = p_html.matcher(value);
		if(matcher.find()) {
			return String.format("<xmp>%s</xmp>", value);
		}
		return value;
	}
	public void setChoiceList(LinkedHashMap<String, String> choiceList) {
		this.choiceList = choiceList;
	}

	public LinkedHashMap<String, String> getChoiceImgList() {
		return choiceImgList;
	}

	public void setChoiceImgList(LinkedHashMap<String, String> choiceImgList) {
		this.choiceImgList = choiceImgList;
	}

}
